package cn.wolfcode.trip.admin.Controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.query.TravelQuery;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 游记资源控制器
 */
@Controller
@RequestMapping("/travel")
public class TravelController {

    @Autowired
    private ITravelService travelService;

    //查出所有已发布游记
    @RequestMapping("/releaseList")
    public String releaseList(TravelQuery qo,Model model){
        qo.setOrderBy("t.releaseTime desc");
        qo.setState(Travel.STATE_RELEASE);
        List<Travel> list = travelService.query(qo);
        PageInfo<Travel> pageInfo =new PageInfo<Travel>(list);
        model.addAttribute("pageInfo",pageInfo);
        return "travel/releaseList";
    }


    //查出所有待审核和已拒绝的游记
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") TravelQuery qo, Model model){
        qo.setIsPublic(true);
        qo.setOrderBy("t.createTime");
        if (qo.getState() == null) {
            qo.setState(Travel.STATE_AUDIT);
        }
        List<Travel> list = travelService.query(qo);
        PageInfo<Travel> pageInfo =new PageInfo<Travel>(list);
        model.addAttribute("pageInfo",pageInfo);
        return "travel/list";
    }

    /**
     * 更新状态
     * @param travel
     * @return
     */
    //发布/拒绝
    @RequestMapping("/updateState")
    @ResponseBody
    public JSONResult updateState(Travel travel){
        JSONResult jsonResult =new JSONResult() ;
        try {
            travelService.updateState(travel);
        } catch (Exception e) {
            jsonResult.mark("操作失败,稍后再试");
            e.printStackTrace();
        }

        return jsonResult;
    }

    //根据游记的id查出这篇游记的内容
    @RequestMapping("/selectContentByTravelId")
    @ResponseBody
    public TravelContent selectContentByTravelId(Long id){

        TravelContent travelContent=travelService.selectContentByTravelId(id);

        return travelContent;
    }
}
